package com.feiyisys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feiyisys.common.PageResult;
import com.feiyisys.common.Result;
import com.feiyisys.entity.LikeCulturalHeritage;
import com.feiyisys.service.LikeCulturalHeritageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户点赞表 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/like")
public class LikeCulturalHeritageController {

    @Autowired
    private LikeCulturalHeritageService likeCulturalHeritageService;
    /**
     * 分页获取用户点赞列表
     */
    @GetMapping("/user/{userId}")
    public PageResult<LikeCulturalHeritage> getUserLikes(
            @PathVariable Long userId,
            @RequestParam(defaultValue = "1") Long current,
            @RequestParam(defaultValue = "10") Long size) {
        Page<LikeCulturalHeritage> page = new Page<>(current, size);
        IPage<LikeCulturalHeritage> result = likeCulturalHeritageService.page(page,
                new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<LikeCulturalHeritage>()
                        .eq("user_id", userId));
        return PageResult.success(result);
    }

    /**
     * 点赞
     */
    @PostMapping("/add")
    public Result<Boolean> addLike(@RequestBody LikeCulturalHeritage like) {
        boolean saved = likeCulturalHeritageService.save(like);
        if (saved) {
            return Result.success(saved);
        } else {
            return Result.fail("点赞失败");
        }
    }

    /**
     * 取消点赞
     */
    @DeleteMapping
    public Result<Boolean> removeLike(@RequestParam Long userId, @RequestParam Long culturalHeritageId) {
        boolean removed = likeCulturalHeritageService.remove(new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<LikeCulturalHeritage>()
                .eq("user_id", userId)
                .eq("cultural_heritage_id", culturalHeritageId));
        if (removed) {
            return Result.success(removed);
        } else {
            return Result.fail("取消点赞失败");
        }
    }
    /**
     * 检查用户是否已点赞指定文化遗产
     */
    @GetMapping("/check")
    public Result<Boolean> checkIsLiked(@RequestParam Long userId, @RequestParam Long culturalHeritageId) {
        boolean exists = likeCulturalHeritageService.count(
                new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<LikeCulturalHeritage>()
                        .eq("user_id", userId)
                        .eq("cultural_heritage_id", culturalHeritageId)) > 0;
        return Result.success(exists);
    }


}
